import random

from core.dependencies import get_redis_service
from db.pydantic.request.sms import UpdateGroupRequest, UpdatePhoneRequest, CreateGroupRequest, \
    SendSmsPhoneRequest, SendSmsGroupRequest
from utils.result import Result
from utils.sms_util import store_phone_to_group, create_sms_group, search_phone_from_group, update_phone_from_group, \
    delete_phone_from_group, search_group, update_group, delete_group, get_all_group, send_sms_by_phone, \
    send_sms_by_group, get_sms


class SMSService:
    """短信服务"""
    def __init__(self):
        pass
    async def create_group(self,create_req:CreateGroupRequest):
        res = await create_sms_group(create_req)
        if res["status"] == "success":
            return Result.success()
        return Result.fail(msg=res.message)

    async def store_phone(self,group_id:str,sr :UpdatePhoneRequest):
        res = await store_phone_to_group(group_id,sr)
        if res["status"] == "success":
            return Result.success(data=res["data"],msg=res.get("message","成功") if res.get("message") else "操作成功")
        return Result.fail(msg=res["message"])

    async def search_phone(self,group_id,uid= None):
        res = await search_phone_from_group(group_id,uid)
        if res["status"] == "success":
            return Result.success(data=res["data"],msg=res.get("message","成功") if res.get("message") else "操作成功")
        return Result.fail(msg=res["message"])

    async def search_group(self,group_id):
        res = await search_group(group_id)
        if res["status"] == "success":
            return Result.success(data=res["data"],msg=res.get("message","成功") if res.get("message") else "操作成功")
        return Result.fail(msg=res["message"])

    async def update_phone(self,group_id,uid,update_req:UpdatePhoneRequest):
        res = await update_phone_from_group(group_id,uid,update_req)
        if res["status"] == "success":
            return Result.success(data=res["data"],msg=res.get("message","成功") if res.get("message") else "操作成功")
        return Result.fail(msg=res["message"])

    async def update_group(self,group_id,update_req:UpdateGroupRequest):
        res = await update_group(group_id,update_req)
        if res["status"] == "success":
            return Result.success(data=res["data"],msg=res.get("message","成功") if res.get("message") else "操作成功")
        return Result.fail(msg=res["message"])

    async def delete_phone(self,group_id,uid):
        res = await delete_phone_from_group(group_id,uid)
        if res["status"] == "success":
            return Result.success(data=res["data"],msg=res.get("message","成功") if res.get("message") else "操作成功")
        return Result.fail(msg=res["message"])

    async def delete_group(self,group_id):
        res = await delete_group(group_id)
        if res["status"] == "success":
            return Result.success(data=res["data"],msg=res.get("message","成功") if res.get("message") else "操作成功")
        return Result.fail(msg=res["message"])

    async def get_group(self):
        res = await get_all_group()
        if res["status"] == "success":
            return Result.success(data=res["data"],msg=res.get("message","成功") if res.get("message") else "操作成功")
        return Result.fail(msg=res["message"])

    async def send_by_phone(self,send_req:SendSmsPhoneRequest):
        redis_server = await get_redis_service()
        await redis_server.init_redis()
        send_req.message = str(random.randint(100000, 999999))
        res = await send_sms_by_phone(send_req)
        status_set  = res["Response"].get("SendStatusSet")
        fail_count = 0
        if status_set:
            for i in status_set:
                if i["Code"] != "Ok":
                    fail_count += 1
                else:#发送成功的短信    记录手机号对应的验证码到redis中，后续对校验
                    phone_number = i["PhoneNumber"].replace("+86","")
                    await redis_server.set_phone_validate_code(phone_number,send_req.message)
            return Result.success(data=res["Response"]["SendStatusSet"],msg=f"操作成功,成功{len(status_set)-fail_count}条,失败{fail_count}条")
        return Result.fail(msg="发送失败")

    async def send_by_group(self,send_req:SendSmsGroupRequest):
        res = await send_sms_by_group(send_req)
        if res["status"] == "success":
            return Result.success(data=res["data"],msg=res.get("message","成功") if res.get("message") else "操作成功")
        return Result.fail(msg=res["message"])
    async def get_sms(self,uid=None):
        if uid:
            res = await get_sms(uid)
        else:
            res = await get_sms()
        if res["status"] == "success":
            return Result.success(data=res["data"],msg=res.get("message","成功") if res.get("message") else "操作成功")
        return Result.fail(msg=res["message"])
async def get_SMSService():
    return SMSService()